package com.kuang.controller;

import com.kuang.pojo.Books;
import com.kuang.pojo.Movies;
import com.kuang.pojo.Users;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @auther liuyang
 * @create 2021-10-24-19:15
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list", list);
        return "allBook";
    }
    @RequestMapping("/movie")
    public String list1(Model model) {
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list", list);
        return "hello_movie";
    }
    @RequestMapping("/views")
    public String list2(Model model) {
        List<Movies> movies1 = bookService.getMovies();
        model.addAttribute("find",movies1);
        return "find_movie";
    }
    @RequestMapping("/view")
    public String list3(Model model,HttpServletRequest request) {
        String movie=request.getParameter("movie");
        List<Movies> movies2 = bookService.searchMovies(movie);
        model.addAttribute("search",movies2);
        return "search_movie";
    }
    @RequestMapping("/enter")
    public String list4(Model model, HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Users users = bookService.testLogin(username,password);
        if(users==null){
            model.addAttribute("msg","登录失败");
            return "login";
        }

        if(users.getUsername().equals(username)&&users.getPassword().equals(password)){
            System.out.println(333);
            HttpSession session= request.getSession();
            session.setAttribute("userLogin",users);

            return "hello_movie";
        }


        return username;
    }
    @RequestMapping("/hrefs")
    public String list5(Model model) {

        return "login";
    }
    @RequestMapping("/toAddBook")
    public String toAddPaper() {
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addPaper(Books books) {
        System.out.println(books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model, int id) {
        Books books = bookService.queryBookById(id);
        System.out.println(books);
        model.addAttribute("book",books );
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Model model, Books book) {
        System.out.println(book);
        bookService.updateBook(book);
        Books books = bookService.queryBookById(book.getBookID());
        model.addAttribute("books", books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
}